﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;

/// <summary>
/// Summary description for Class1
/// </summary>
public class SendSMS
{
	public SendSMS()
	{
		//
		// TODO: Add constructor logic here
		//
	}
    public static bool sendSMS(string LoginID, string Masking, string No, string SMS)
    {
        try
        {
            int noOfSMS = countSMS(SMS);
            Masking = processMasking(Masking);
            No = processNo(No);
            SMS = processSMS(SMS);

            if (No != "")
            {
                MyWebRequest myRequest = new MyWebRequest("http://api.wantechbd.com/api/v3/sendsms/plain?user=anam_iut&password=ahasan38&sender=" + Masking + @"&SMSText=" + SMS + "&GSM=" + No + (noOfSMS != 1 ? "&&type=longSMS" : ""));
                XmlDocument doc = new XmlDocument();

                string successMessage = "";
                doc.LoadXml(myRequest.GetResponse());
                XmlNodeList nodesUrl = doc.SelectNodes("status");
                if (doc.InnerText.ToString().Contains("0"))
                {
                    string sql = @"update Login set RemainingSMS-=" + noOfSMS.ToString() + " where LoginID="+LoginID.ToString();
                    DatabaseManager.ExecSQL(sql);
                    return true;
                }
                else
                {
                    return false;
                }
            }
            
        }
        catch (Exception ex)
        { 
            return false;
        }
        return false;
    }

    public static int remainingSMS(string LoginID)
    {
        string sql = @"select RemainingSMS from Login where LoginID="+LoginID;
        try
        {
            return int.Parse(DatabaseManager.ExecSQL(sql).Tables[0].Rows[0][0].ToString());
        }
        catch (Exception ex)
        { }
        return 0;
    }


    public static int countSMS(string SMS)
    {
        if (SMS.Length <= 160) return 1;
        else
        {
            return int.Parse(decimal.Parse((SMS.Length / 150).ToString()).ToString("0")) + 1;
        }
    }

    private static string processSMS(string SMS)
    {
        if (SMS.Contains('&'))
           return SMS.Replace("&", "%26");
        else
            return SMS;
    }

    private static string processNo(string No)
    {
        if (No.Substring(0, 1) == "0") return "88"+No;
        else
            if (No.Length == 10) return "880" + No;
            else if (No.Substring(0, 3) == "+88" && No.Length == 14)
            {
                return No.Replace("+", "");
            }
            else if (No.Substring(0, 2) == "88" && No.Length == 13)
            {
                return No;
            }
            else
                return "";
    }

    private static string processMasking(string Masking)
    {
        if (Masking.Length <= 11) return Masking;
        else
            return Masking.Substring(0, 11);
    }
}